package org.xhy.retryer.core;

import org.xhy.retryer.lister.RetryerLister;
import org.xhy.retryer.strategy.RetryerStrategy;

import java.util.concurrent.ExecutorService;
import java.util.function.Predicate;

/**
 * @description:
 * - 支持重试策略 ： 不重试，重试次数，重试时间，重试指数级时间 使用抽象类
 * - 支持根据返回结果是否需要重试
 * - 支持抛出异常是否重试
 * - 支持重试执行策略(同步异步)
 * - 支持重试监听器
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-04-29 21:29
 */
public class RetryerBuilder<T> {
    private RetryerStrategy retryerStrategy;

    private Predicate predicateResult;

    private Predicate predicateThrowsException;

    private RetryerLister retryerLister;

    private ExecutorService executorService;

    public static RetryerBuilder newBuilder(){
        return new RetryerBuilder();
    }

    public RetryerBuilder withRetryerStrategy(RetryerStrategy retryerStrategy){
        this.retryerStrategy = retryerStrategy;
        return this;
    }

    public RetryerBuilder retryerIfThrowsException(Predicate predicate){
        this.predicateThrowsException = predicate;
        return this;
    }

    public RetryerBuilder retryerIfResult(Predicate predicate){
        this.predicateResult = predicate;
        return this;
    }

    public RetryerBuilder retryerExecStrategy(ExecutorService executorService){
        this.executorService = executorService;
        return this;
    }

    public RetryerBuilder retryerLister(RetryerLister retryerLister){
        this.retryerLister = retryerLister;
        return this;
    }

    public Retryer build(){
        final Retryer retryer = new Retryer(retryerStrategy, predicateResult, predicateThrowsException,retryerLister,executorService);
        return retryer;
    }
}
